<template>
  <view>
    <!--#ifdef APP-PLUS-->
    <view class="lz-status_bar">
      <view class="lz-top_view"></view>
    </view>
    <!--#endif-->
    <!-- #ifndef MP-WEIXIN -->
    <view class="kaoshi-head-top">
      <view class="kaoshi-head-left">
        <navigator open-type="navigateBack" @tap="$navigateBack">
          <view class="iconfont icon-zuojiantou"></view>
        </navigator>
      </view>
    </view>
    <!--#endif-->
    <view class="kaoshi-subject">
      <view class="kaoshi-subject-title">
        <view>全部科目</view>
        <view class="kaoshi-subject-about">点击切换科目</view>
      </view>
	  
	  <view class="me">
		<view v-for="(item, index) in major">
			<button class="op" @click="chooseFirst(index)">{{ index }}</button>
		</view>
	  	
	  </view>

	  <view class="options" v-if="list">
	  	<view v-for="(item, index) in list" :key="index"
			class="op" :class="{ 'kaoshi-subject-active' : Subject_id == item.id }"
	  		@tap="tap_handle_choice_subject(list[index])">
	  		<view>{{ item.subject_name }}</view>
	  	</view>
	  </view>

	  
<!--     <view class="kaoshi-subject-content">
		<view v-for="(item, index) in list" :key="index"
			class="kaoshi-subject-content-flex" :class="{ 'kaoshi-subject-active' : Subject_id == item.id }"
			@tap="tap_handle_choice_subject(list[index])">
			<view>{{ item.subject_name }}</view>
		</view>
      </view> -->
	  
	  
	  
    </view>
  </view>
</template>
<script>
import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue'
import { mapState } from 'vuex'
export default {
  data () {
    return {
	  current_major: ''	,// 当前专业
	  major: {},    // 专业
	  all_list: [],	//所有科目
      list: [],		//当前专业科目列表
      Subject_id: 0,//已选的科目id
	  currentClass: {} // 当前选择的分类
    }
  },
  onShow () {
    // 获取科目列表
    this.getSub()
    this.Subject_id = this.subject.id
  },
  computed: {
    ...mapState(['subject'])
  },
  components: {
    uniNavBar
  },
  methods: {
    async getSub () {
		// uni.request({
		// 	url: 'https://www.lqwh.club/check/class',
		// 	success: (res) => {
		// 	                        console.log('get成功');
		// 	                        console.log(res);
		// 							this.list = res.data;
		// 	                    },
		// })
		
		
      // let res = await this.$myHttp.post({
      //   url: this.$myHttp.urlMap.subjectList,
      //   data: {
      //     page:1,
      //     limit:0
      //   }
      // })
	  let res = await this.$myHttp.get({
	    url: this.$myHttp.urlMap.subjectList,
	  })
      if (res.code === 1) {
        // this.list = res.data.data;
		this.all_list = res.data.data;
		
		for (let i in this.all_list) {
			
			let name = this.all_list[i].subject_name;
			
			let major = name.slice(name.search('【')+1, name.search('】'));
			if (i == 1){
				this.current_major = major;
			}
			name = name.slice(name.search('】') + 1)

			// console.log(name + '->' + major);
			try{
				this.major[major].push({subject_name: name, id: this.all_list[i].id});
			}catch(e){
				this.major[major] = [{subject_name: name, id: this.all_list[i].id}];
			}
			
		}
		
		this.list = this.major[this.current_major];
		// console.log();
		// console.log(this.major);


		
      }
    },
	chooseFirst(classObj){
		// 点击一级分类
		this.current_major = classObj;
		this.list = this.major[classObj];
		
	},
	choose(item, classname){
		console.log(item.id);
		this.$store.commit('setSubject', { subject: { id: item.id, subject_name: '【'+ this.current_major + '】' + classname } })
		this.$myUtils.$router.switchTab({
		  url: '/pages/index/index'
		})
	}
	
	,
    tap_handle_choice_subject (item) {
	
      this.$store.commit('setSubject', { subject: { id: item.id, subject_name: '【'+ this.current_major + '】' + item.subject_name } })
      this.$myUtils.$router.switchTab({
        url: '/pages/index/index'
      })
    }
  }
}
</script>
<style>
  page{background: #fff}
  .kaoshi-head-left .icon-zuojiantou {
      color: #888;
  }
  .kaoshi-subject {
    padding-top: 12px;
    background: #fff;
  }
  .kaoshi-subject-title{
    width: 88%;
    font-size: 20px;
    font-weight: bold;
    color: #3c464f;
    margin: 0 auto;
  }
  .kaoshi-subject-about{
    font-size: 14px;
    color: #bbb;
    font-weight: normal;
    margin: 4px 0;
  }
  .kaoshi-subject-content {

	display: inline-block;
	margin-left: 50px;
    width: 40%;
	border: 0px;
	padding: 0px;
    margin: 0;

    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
  .kaoshi-subject-content-flex {
	display:inline-block;
    /* display: flex; */
	margin-left: 250px;
    align-items: center;
    justify-content: center;
    background-color:#f3f3f3;
    color: #4a4a4a;
    min-width: 50%;
	/* max-width: 80%; */
    padding: 12px 16px;
    font-size: 16px;
    border-radius: 34px;
    margin-bottom: 24px;
  }
  .kaoshi-subject-active {
    background-color: #3c7bfc;
    color: #fff;
  }
  
  .options{
	  border-left: 1px solid #00BFFF;
	  vertical-align:top;
	  display: inline-block;
	  width: 40%;
	  

  }
  .me{
	  /* vertical-align:middle; */
	  /* height: 800px; */
	  
	  display: inline-block;
	  width: 50%;
	  
  }
  .op{
  	 	display:inline-block;
  	     /* display: flex; */
		margin-left: 24px;
  	     align-items: center;
  	     justify-content: center;
  	     background-color:#f3f3f3;
  	     color: #4a4a4a;
  	     min-width: 50%;
  	 	/* max-width: 80%; */
  	     padding: 12px 16px;
  	     font-size: 16px;
  	     border-radius: 34px;
  	     margin-bottom: 24px;
  }
  .btn {
  	/* width: calc(50% - 60rpx); */
  	/* height: 60rpx; */
	/* display: inline-block; */
	
	width: 80%;
	margin: 10px;
	
/*  	line-height: 60rpx;
  	margin: 20rpx 30rpx;
  	padding: 0 20rpx;
  	border-color: #999999;
  	border-width: 2rpx;
  	border-radius: 20rpx;
  	border-style: solid;
  	font-size: 26rpx;
  	text-align: center;
  	background-color: #FFFFFF; */
  }
  .line {
  	width: 100%;
  	height: 10rpx;
  	background-color: #f8f8f8;
  }

</style>
